package com.atguigu.ucenterService.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.atguigu.commonUtils.JwtUtils;
import com.atguigu.ucenterService.entity.UcenterMember;
import com.atguigu.ucenterService.mapper.UcenterMemberMapper;
import com.atguigu.ucenterService.service.UcenterMemberService;
import com.atguigu.ucenterService.vo.RegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-08-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Resource
    private UcenterMemberMapper ucenterMemberMapper;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    //用户登录
    @Override
    public String login(UcenterMember ucenterMember) {
        if (StringUtils.isEmpty(ucenterMember.getMobile()) || StringUtils.isEmpty(ucenterMember.getPassword())) {
            throw new RuntimeException();
        }
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",ucenterMember.getMobile());
        UcenterMember member = ucenterMemberMapper.selectOne(ucenterMemberQueryWrapper);
        if (member == null) {
            throw new RuntimeException();
        }
        if (!SecureUtil.sha1(ucenterMember.getPassword()).equals(member.getPassword())) {
            throw new RuntimeException();
        }
        if (member.getIsDisabled()) {
            throw new RuntimeException();
        }
        //获取token
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }


    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        if (StringUtils.isEmpty(registerVo.getMobile()) || StringUtils.isEmpty(registerVo.getPassword()) || StringUtils.isEmpty(registerVo.getCode()) || StringUtils.isEmpty(registerVo.getNickname())) {
            throw new RuntimeException();
        }
        String code = redisTemplate.opsForValue().get(registerVo.getMobile());  //从redis中获取验证码
        if (!registerVo.getCode().equals(code)) {
            throw new RuntimeException();
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",registerVo.getMobile());
        Integer count = ucenterMemberMapper.selectCount(ucenterMemberQueryWrapper);
        if (count > 0) {
            throw new RuntimeException();
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setPassword(SecureUtil.sha1(registerVo.getPassword()));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://guli-edu20210721.oss-cn-beijing.aliyuncs.com/teacher_head/c2df77441343474892f38f0c5eab3765.png");
        ucenterMemberMapper.insert(ucenterMember);
    }

    //根据token获取用户信息
    @Override
    public UcenterMember getMemberInfo(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        return ucenterMemberMapper.selectById(id);
    }

    //根据openid获取用户信息
    @Override
    public UcenterMember getUserByOpenId(String openid) {
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("openid",openid);
        return ucenterMemberMapper.selectOne(ucenterMemberQueryWrapper);
    }
}
